/**
 * Project Name:cnsy-url-short-service
 * File Name:CnsyDefaultMultipartResolver.java
 * Package Name:cn.cnsy123.shorturls.business.resolver 
 * Date: 2020年4月8日-上午12:27:26
 * Copyright (c) 2020, yangpengfei@360@126.com All Rights Reserved. 
 *
 */

package cn.cnsy123.shorturls.business.file.resolver;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import cn.cnsy123.shorturls.business.file.progress.listener.CnsyDefaultProgressListener;

/**
 * ClassName: CnsyDefaultMultipartResolver
 * 
 * @author yangpengfei
 * @date 2020年4月8日-上午12:27:26
 * @version 1.0
 * @since JDK 1.7
 */
public class CnsyDefaultMultipartResolver extends CommonsMultipartResolver {
    
    private HttpServletRequest request;

    @Override
    protected FileUpload newFileUpload(FileItemFactory fileItemFactory) {
        ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
        if(request != null) {
            servletFileUpload.setProgressListener(new CnsyDefaultProgressListener(request.getSession())); 
        }
        return servletFileUpload;
    }

    @Override
    public MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException {
        this.request = request;
        return super.resolveMultipart(request);
    }

    @Override
    protected MultipartParsingResult parseRequest(HttpServletRequest request) throws MultipartException {
        String encoding = determineEncoding(request);
        FileUpload fileUpload = prepareFileUpload(encoding);
        CnsyDefaultProgressListener progressListener = new CnsyDefaultProgressListener(request.getSession());
        fileUpload.setProgressListener(progressListener);  
        try {
            List<FileItem> fileItems = ((ServletFileUpload) fileUpload).parseRequest(request);
            return parseFileItems(fileItems, encoding);
        }
        catch (FileUploadBase.SizeLimitExceededException ex) {
            throw new MaxUploadSizeExceededException(fileUpload.getSizeMax(), ex);
        }
        catch (FileUploadBase.FileSizeLimitExceededException ex) {
            throw new MaxUploadSizeExceededException(fileUpload.getFileSizeMax(), ex);
        }
        catch (FileUploadException ex) {
            throw new MultipartException("Failed to parse multipart servlet request", ex);
        }
    }
    
    

}
